package com.lintf.batch.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lintf.batch.domain.Student;
import com.lintf.batch.mapper.StudentMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class StudentService extends ServiceImpl<StudentMapper, Student> {
    private final AtomicInteger processedCount = new AtomicInteger(0);
    private final AtomicInteger errorCount = new AtomicInteger(0);

    public void saveStudents(List<Student> students) {
        if (students != null && !students.isEmpty()) {
            saveBatch(students);
            processedCount.addAndGet(students.size());
        }
    }

    public boolean existsById(String studentId) {
        return lambdaQuery().eq(Student::getStudentId, studentId).count() > 0;
    }

    public int getProcessedCount() {
        return processedCount.get();
    }

    public int getErrorCount() {
        return errorCount.get();
    }

    public void recordError() {
        errorCount.incrementAndGet();
    }

    public void recordError(String message, Throwable e) {
        errorCount.incrementAndGet();
        log.error(message, e);
    }

    public void resetCounters() {
        processedCount.set(0);
        errorCount.set(0);
    }
}